import * as React from 'react';
import classNames from 'classnames';
import Pager from './Pager';

function noop() {}

export interface PaginationProps {
	children?: React.ReactNode;
	className?: string;
	defaultCurrent?: number;
	total?: number;
	defaultPageSize?: number;
	current?: number;
	pageSize?: number;
	prefixCls?: string;

	//onChange?: (page: number, pageSize: number) => void;
}

export interface PaginationState {
	current: number;
	pageSize: number;
}

//获取最大页数
function calculatePage(p:any, state:any, props:any) {
	const pageSize = typeof p === 'undefined' ? state.pageSize : p;
	return Math.floor((props.total - 1) / pageSize) + 1;
}

class Pagination extends React.Component<PaginationProps, PaginationState> {
	static defaultProps = {
		defaultCurrent: 1,
		total: 0,
		defaultPageSize: 10,
		className: '',
		prefixCls: 'bh-pagination',

		//onChange: noop,
	};

	constructor(props:PaginationProps) {
		super(props);
		const current = props.current || props.defaultCurrent || 1;
		const pageSize = props.pageSize || props.defaultPageSize || 10;

		this.state = {
			current,
			pageSize,
		};
	}

	hasPrev = () => {
		return this.state.current > 1;
	}
	// 点击上一步
	prev = () => {
		const current = this.state.current;

		if(this.hasPrev()) {
			//const { onChange } = this.props;
			let newPage = this.state.current - 1;
			this.setState({
				current: newPage,
			});
			//onChange && onChange(newPage, this.state.pageSize);
		}
	}

	hasNext = () => {
		return this.state.current < calculatePage(undefined, this.state, this.props);
	}
	// 点击下一步
	next = () => {
		if(this.hasNext()) {
			//const { onChange } = this.props;
			let newPage = this.state.current + 1;
			this.setState({
				current: newPage,
			});
			//onChange && onChange(newPage, this.state.pageSize);
		}
	}

	// 点击页码
	handleChange = (page:number) => {
		if(page !== this.state.current) {
			//const { onChange } = this.props;
			this.setState({
				current: page,
			})
			//onChange && onChange(page, this.state.pageSize);
		}
	}
	render () {
		const {
			children,
			className,
			defaultCurrent,
			total,
			defaultPageSize,
			prefixCls,
			...restProps
		} = this.props;
		const { current, pageSize } = this.state;

		const classString = classNames(
			prefixCls,
			className,
		);

		const allPages = calculatePage(undefined, this.state, this.props);
		const pagerList = [];
		const prevDisabled = !this.hasPrev() || !allPages;
		const nextDisabled = !this.hasNext() || !allPages;
		
		for(let i = 1; i <= allPages; i++) {
			const active = current === i;
			pagerList.push(<Pager 
				rootPrefixCls={prefixCls} 
				active={active}
				key={i} 
				onClick={this.handleChange}
				page={i} />);
		}

		return (
			<ul {...restProps} className={classString}>
			    <li 
			        onClick={this.prev}
			        className={classNames(`${prefixCls}-prev`, {
			        	[`${prefixCls}-disabled`]: prevDisabled,
					})}
			        title="上一页">
			        <button className="bh-pagination-item-link">
			        	<i className="bh-icon bh-icon-arrow-left"></i>
			        </button>
			    </li>
			    {pagerList}
			    <li
			        onClick={this.next} 
			        className={classNames(`${prefixCls}-next`, {
			        	[`${prefixCls}-disabled`]: nextDisabled,
					})}
			        title="下一页">
			        <button className="bh-pagination-item-link">
			        	<i className="bh-icon bh-icon-arrow-right"></i>
			        </button>
			    </li>
			</ul>
		)
	}
};

export default Pagination;